<template>
    <div class="big_box">
        <el-breadcrumb separator="/">
            <el-breadcrumb-item>公章管理</el-breadcrumb-item>
            <el-breadcrumb-item>公章维护</el-breadcrumb-item>
        </el-breadcrumb>
        <el-divider></el-divider>
        <el-form :inline="true" :model="selectfrom" ref="_selectfrom"  class="demo-form-inline">
            <el-row :gutter="5">
                    <el-form-item label="名称" style="white-space:nowrap;">
                        <el-input style="width: 150px" placeholder="名称" v-model="selectfrom.Name"></el-input>
                    </el-form-item>
                    <el-form-item label="保管人" style="white-space:nowrap;">
                        <el-select style="width: 150px" placeholder="保管人" v-model="selectfrom.CustodianId" filterable>
                            <el-option label="全部" value=""></el-option>
                            <el-option v-for="v in $store.state.AllUsers" v-if="v.InternshipState!=8"
                                       :label="v.UserName" :value="v.Id"></el-option>
                        </el-select>
                    </el-form-item>
                <el-button plain type="success" icon="el-icon-search" @click="formdata"></el-button>
            </el-row>
        </el-form>
        <el-row style="padding-bottom: 10px;">
            <el-col>
                <el-button-group>
                    <el-button class="buttongroup" type="danger" icon="el-icon-delete" @click="BatchDelete_event">批量删除
                    </el-button>
                    <el-button class="buttongroup" type="success" icon="el-icon-circle-plus-outline"
                               @click="Add_OfficialSealAdmin_dialog=true">添加公章
                    </el-button>
                </el-button-group>
            </el-col>
        </el-row>

        <el-row>
            <el-col :span="24">
                <el-table :data="tableData" border stripe style="width: 100%" @selection-change="SelectionChange_event">
                    <el-table-column type="selection" width="40" align="center"></el-table-column>
                    <el-table-column fixed type="index" label="序号" width="50" align="center"
                                     :index="indexMethod"></el-table-column>
                    <el-table-column prop="Name" label="名称" align="center" show-overflow-tooltip></el-table-column>
                    <el-table-column prop="CustodianId" label="保管人" show-overflow-tooltip align="center">
                        <template slot-scope="slot">
                            {{ GetUserName(slot.row.CustodianId) }}
                        </template>
                    </el-table-column>
                    <el-table-column fixed="right" label="操作" width="320" align="center">
                        <template slot-scope="scope">
                            <el-button plain @click="handleClick(scope.row,false)" type="success" size="mini">编辑
                            </el-button>
                            <el-button plain type="danger" size="mini" @click="BatchDeleteOne_event(scope.row)">删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
            <el-col :span="24">
                <div class="block">
                    <el-pagination
                            @size-change="formdata"
                            @current-change="formdata"
                            :current-page.sync="pagination.currentPage"
                            :page-sizes="pagination.pagesizes"
                            :page-size.sync="pagination.pagesize"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total.sync="pagination.total">
                    </el-pagination>
                </div>
            </el-col>
        </el-row>

        <el-dialog :visible.sync="Add_OfficialSealAdmin_dialog" title="添加公章" top="10px" width="85%"
                   @close="AddOfficialSealAdminCloseEvent">
            <AddOfficialSealAdmin ref="AddOfficialSealAdmin_refs"></AddOfficialSealAdmin>
        </el-dialog>
        <el-dialog :visible.sync="Update_OfficialSealAdmin.dialog" top="10px"
                   :title="'修改公章---'+Update_OfficialSealAdmin.title" width="85%">
            <UpdateOfficialSealAdmin :is-see="IsSee"
                                     :OfficialSealAdmin="OfficialSealAdminDetails"></UpdateOfficialSealAdmin>
        </el-dialog>
    </div>
</template>

<script>
    import * as publicjs from "../../../../public/Scriput/Public";
    import AddOfficialSealAdmin from "./AddOfficialSealAdmin";
    import UpdateOfficialSealAdmin from "./UpdateOfficialSealAdmin";


    export default {
        name: "OfficialSealAdmin",
        components: {
            'AddOfficialSealAdmin': AddOfficialSealAdmin,
            'UpdateOfficialSealAdmin': UpdateOfficialSealAdmin
        },
        data() {
            return {
                //表格查询数据
                selectfrom: {
                    Id: "",
                    Name: "",
                    CustodianId: "",
                },
                //表格数据
                tableData: [],
                //分页数据绑定
                pagination: {
                    currentPage: 1,
                    pagesizes: [10, 20, 30, 40, 50],
                    pagesize: 10,
                    total: 100
                },
                //用于表示查看/编辑
                IsSee: false,
                //新建公章弹框
                Add_OfficialSealAdmin_dialog: false,
                //多选列表的数据集合
                SelectMore_lis: [],
                //公章审批表格弹框
                Update_OfficialSealAdmin: {
                    dialog: false,
                    title: '',
                },
                //公章详情数据,用于编辑
                OfficialSealAdminDetails: {},
            }
        },
        created() {
            this.formdata()
        },
        methods: {
            frmdate(obj) {
                return obj.split(' ')[0]
            },
            //表格数据选择事件
            SelectionChange_event(value) {
                this.SelectMore_lis = value
            },
            //获取对应人员名字
            GetUserName(value) {
                let name = ''
                if (value != 0) {
                    for (let i = 0; i < this.$store.state.AllUsers.length; i++) {
                        if (this.$store.state.AllUsers[i].Id == value) {
                            name = this.$store.state.AllUsers[i].UserName
                            return name
                        }
                    }
                }
            },
            //表格数据序列号
            indexMethod(index) {
                return ((this.pagination.currentPage - 1) * this.pagination.pagesize) + index + 1
            },
            //初始化表格数据加载
            formdata() {
                let loadding = this.openLoading();
                this.$http.post('OfficialSeal/GetOfficialSeal', {
                    currentPage: this.pagination.currentPage,
                    pagesize: this.pagination.pagesize,
                    Id: this.selectfrom.Id,
                    Name: this.selectfrom.Name,
                    CustodianId: this.selectfrom.CustodianId,
                }).then(res => {
                    loadding.close();
                    this.pagination.total = res.data.count
                    this.tableData = res.data.data
                }).catch(() => {
                    loadding.close();
                    this.$message.error('服务器异常,请联系管理员!')
                })
            },
            //表格单个删除
            BatchDeleteOne_event(obj) {
                this.$confirm('你确定要删除吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'error'
                }).then(() => {
                    let loadding = this.openLoading();
                    this.$http.post('OfficialSeal/DeleteOfficialSeal', [obj.Id]).then(res => {
                        loadding.close();
                        if (res.data.msg.IsSuccess) {
                            this.$message.success(res.data.msg.Msg)
                            this.formdata()
                        } else {
                            this.$message.warning(res.data.msg.Msg)
                        }
                    }).catch(() => {
                        loadding.close();
                        this.$message.error('服务器异常,请联系管理员!')
                    })
                }).catch(() => {
                    this.$message.info('已取消!')
                })
            },
            //批量删除
            BatchDelete_event() {
                let Ids = []
                if (this.SelectMore_lis.length == 0) {
                    this.$message.warning('您还未选择需要删除的设备!')
                } else {
                    for (let i = 0; i < this.SelectMore_lis.length; i++) {
                        Ids.push(this.SelectMore_lis[i].Id)
                    }
                    this.$confirm('你确定要删除吗?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'error'
                    }).then(() => {
                        let loadding = this.openLoading();
                        this.$http.post('OfficialSeal/DeleteOfficialSeal', Ids).then(res => {
                            loadding.close();
                            if (res.data.msg.IsSuccess) {
                                this.$message.success(res.data.msg.Msg)
                                this.formdata()
                                this.SelectMore_lis = []
                            } else {
                                this.$message.warning(res.data.msg.Msg)
                            }
                        }).catch(() => {
                            loadding.close();
                            this.$message.error('服务器异常,请联系管理员!')
                        })
                    }).catch(() => {
                        this.$message.info('已取消!')
                        this.SelectMore_lis = []
                    })

                }
            },
            //编辑
            handleClick(obj, Isshow) {
                if (obj.CustodianId === this.$store.state.user.id) {
                    let newdata = JSON.stringify(obj)
                    this.OfficialSealAdminDetails = JSON.parse(newdata)
                    this.Update_OfficialSealAdmin.dialog = true
                    this.Update_OfficialSealAdmin.title = obj.Name
                    this.IsSee = Isshow
                } else {
                    this.$message.warning('操作失败，只能操作本部门或者本人提交的公章')
                }
            },
            //添加申购关闭
            AddOfficialSealAdminCloseEvent() {
                this.$refs['AddOfficialSealAdmin_refs'].dialog_close()
            },
        }
    }
</script>

<style scoped>

</style>